
Partial Class SpecialField_SpecialFieldControl
    Inherits System.Web.UI.UserControl

    Public Property PrimaryObject() As Cdm.DataCore.ConfigurableObject
        Get
            Try
                Return ViewState("primaryobj")
            Catch ex As Exception
                Return Nothing
            End Try
        End Get
        Set(ByVal value As Cdm.DataCore.ConfigurableObject)
            ViewState("primaryobj") = value
            BindDataList(value)
        End Set
    End Property

    Public Property IsBound() As Boolean
        Get
            If ViewState("isbound") = Nothing Then
                Return False
            Else
                Return ViewState("isbound")
            End If
        End Get
        Set(ByVal value As Boolean)
            ViewState("isbound") = value
        End Set
    End Property

    Public Property Editable() As Boolean
        Get
            If ViewState("editable") Is Nothing Then Return True
            Return ViewState("editable")
        End Get
        Set(ByVal value As Boolean)
            ViewState("editable") = value
        End Set
    End Property

    Private Sub BindDataList(ByVal value As Cdm.DataCore.ConfigurableObject)
        Dim fieldList As System.Collections.Generic.List(Of Cdm.DataCore.Admin.SpecialFieldDisplay)
        fieldList = value.SpecialFieldsDisplay
        Me.dlSpecialFields.DataSource = fieldList
        Me.dlSpecialFields.DataBind()
        Me.IsBound = True
    End Sub


    Public Function GetDataSource(ByVal fieldId As Integer, ByVal hasDataSource As Boolean) As Object
        If hasDataSource = False Then
            Return Nothing
        Else
            Dim listItems As System.Collections.Generic.List(Of Cdm.DataCore.Admin.AdminSpecialFieldListItems)
            listItems = Cdm.DataCore.Admin.AdminSpecialFieldListItems.ListByField(fieldId, "FieldValue")
            Return listItems
        End If
    End Function

    Public Function SaveSpecialFields() As System.Collections.Generic.List(Of Cdm.DataCore.Admin.SpecialFieldValue)
        If Me.Editable = True Then
            Dim i As Int32
            Dim genericCtl As InputControls.GenericInputControl
            Dim specialFieldName As String
            Dim specialFieldValue As String
            For i = 0 To Me.dlSpecialFields.Items.Count - 1
                genericCtl = dlSpecialFields.Items(i).FindControl("gic1")
                specialFieldName = genericCtl.BoundProperty
                specialFieldValue = genericCtl.Value
                Me.PrimaryObject.SetSpecialField(specialFieldName, specialFieldValue)
            Next
        End If
        Return Me.PrimaryObject.SpecialFieldsValues
    End Function

    Protected Sub dlSpecialFields_ItemDataBound1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles dlSpecialFields.ItemDataBound
        Dim fieldDisplay As Cdm.DataCore.Admin.SpecialFieldDisplay
        Dim genericInputControl As InputControls.GenericInputControl
        fieldDisplay = CType(e.Item.DataItem, Cdm.DataCore.Admin.SpecialFieldDisplay)
        genericInputControl = e.Item.FindControl("gic1")
        genericInputControl.ControlType = fieldDisplay.FieldControlType
        genericInputControl.ToolTip = fieldDisplay.FieldTypeName
        genericInputControl.Title = fieldDisplay.FieldName
        genericInputControl.Required = fieldDisplay.IsRequired
        genericInputControl.RequiredText = "*"
        genericInputControl.cssClassLabel = "label"
        genericInputControl.cssClassControl = "form"
        genericInputControl.BoundProperty = fieldDisplay.FieldName
        genericInputControl.Editable = Me.Editable
        If fieldDisplay.HasDataSource = True Then
            genericInputControl.DataValueField = "SpecialFieldListItemId"
            genericInputControl.DataTextField = "FieldValue"
            genericInputControl.DataSource = GetDataSource(fieldDisplay.SpecialFieldId, fieldDisplay.HasDataSource)
            genericInputControl.DataBind()
            If fieldDisplay.FieldTypeName = "Dropdown" And genericInputControl.Editable Then
                If genericInputControl.Items() Is Nothing Then
                    genericInputControl.Items.Add(New ListItem("...", ""))
                Else
                    genericInputControl.Items.Insert(0, New ListItem("...", ""))
                End If
            End If
        End If
        genericInputControl.IsSpecialFieldControl = True
        If Not Me.IsBound Then
            genericInputControl.Value = Me.PrimaryObject.GetSpecialFieldValue(fieldDisplay.SpecialFieldId)
        End If
    End Sub
End Class
